<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script type="text/javascript"> 
  </script>
  <script type="text/javascript"> 

			function Person(name , age , gender){
				this.name = name;
				this.age = age;
				this.gender = gender;
      }
      
      var per = new Person("孙悟空",18,"男");
      var per2 = new Person("猪八戒",28,"男");
      
      //当我们直接在页面中打印一个对象时，事件上是输出的对象的toString()方法的返回值
      console.log(per.toString()) //[object Object]
      console.log(per) //Person:.....

      // 这个toString方法其实是原型的原型里面的，打印出来是一个object，并没有实际的值
      console.log(per.hasOwnProperty('toString')) // false
      console.log(per.__proto__.hasOwnProperty('toString')) // false
      console.log(per.__proto__.__proto__.hasOwnProperty('toString')) //true

			// 如果我们希望在输出对象时不输出[object Object]，可以为对象添加一个toString()方法
      
      // 最好给对象的原型添加这个方法，这样所有的对象都有了这个方法
      Person.prototype.toString = function () {
        return 'Person[name='+this.name+', age='+this.age+',gender=' +this.gender+']'
      }

			var result = per.toString();
			console.log("result = " + result);
			//console.log(per.__proto__.__proto__.hasOwnProperty("toString"));
			console.log(per2.toString());
			console.log(per);
      //Person[name=孙悟空,age=18,gender=男]
			/*per.toString = function(){
				return "Person[name="+this.name+",age="+this.age+",gender="+this.gender+"]";
			};*/
			
  </script>
  
</head>
<body>
  
</body>
</html>